package zozopo.warehouse;

import java.io.UnsupportedEncodingException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import zozopo.background.operator.operations.OperatorTB;
import zozopo.background.operator.operations.OperatorlimitedTB;

import zozopo.warehouse.operations.GoodsclassOS;
import zozopo.warehouse.operations.GoodsclassTB;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

public class ManageGoodsclassAction extends ActionSupport implements Action,SessionAware{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static final String limitedCode ="WAREHOUSE.GOODS.CLASS";
	
	
	//传入参数
	private String searchStr = "";
	//分页参数
	private int beginPageNum = 0;
	private int pageSize = 20;
	private int rowNum = 0;
	//传出参数
	private List<GoodsclassTB> goodsclassList;
	private LinkedHashMap<Integer,GoodsclassTB> goodsclassMap;
	
	
	private Map<Object, Object> session;
	public void setSession(Map arg0) {
		// TODO Auto-generated method stub
		session = arg0;
	}
	public String execute(){
		GoodsclassOS goodsclassOS = new GoodsclassOS();
		
		goodsclassList = goodsclassOS.select();
		
		goodsclassMap = new  LinkedHashMap<Integer,GoodsclassTB>();
		
		getGoodsclassMap(0);
		
		
		goodsclassOS.releaseConnection();
		
		return SUCCESS;
		
	}
	
	public LinkedHashMap<Integer,GoodsclassTB>  getGoodsclassMap(int parentid){
		
		
		for(GoodsclassTB goodsclass:goodsclassList){
			if(!goodsclassMap.containsKey(goodsclass.getId_goodsclass())){//如果已不存在
				if(goodsclass.getParentid_goodsclass() == parentid){//如果父ID正常
					goodsclassMap.put(goodsclass.getId_goodsclass(), goodsclass); 
					getGoodsclassMap(goodsclass.getId_goodsclass());
				}
			}
		}
		
		return goodsclassMap;
	}
	
	
	public void validate() {
		//权限处理
		OperatorTB operator = (OperatorTB)session.get("operatorInfo"); 
	
		if(operator == null){
			addFieldError("errormessage",getText("operator.timeoutornotlogin"));
			return ; 
		} 
		
		//用户权限
		Map<String,OperatorlimitedTB> operatorlimitedMap = (Map<String,OperatorlimitedTB>)session.get("operatorlimitedMap");
		
		if(operatorlimitedMap.get(limitedCode) == null){
			addFieldError("errormessage","用户未有产品管理权限.");
			return;
		}
		
		
	}
	
	public String getSearchStr() {
		return searchStr;
	}
	
	public void setSearchStr(String searchStr) throws UnsupportedEncodingException {
		this.searchStr = java.net.URLDecoder.decode(searchStr, "UTF-8");
		
	}
	
	
	public int getBeginPageNum() {
		return beginPageNum;
	}
	public void setBeginPageNum(int beginPageNum) {
		this.beginPageNum = beginPageNum;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public int getRowNum() {
		return rowNum;
	}
	public void setRowNum(int rowNum) {
		this.rowNum = rowNum;
	}
	
	public static String getLimitedcode() {
		return limitedCode;
	}
	public List<GoodsclassTB> getGoodsclassList() {
		return goodsclassList;
	}
	public void setGoodsclassList(List<GoodsclassTB> goodsclassList) {
		this.goodsclassList = goodsclassList;
	}
	public LinkedHashMap<Integer, GoodsclassTB> getGoodsclassMap() {
		return goodsclassMap;
	}
	public void setGoodsclassMap(LinkedHashMap<Integer, GoodsclassTB> goodsclassMap) {
		this.goodsclassMap = goodsclassMap;
	}

}
